﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Services.Description;
using System.Web.Services;
using System.Net;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Diagnostics;


namespace CrmServiceGenerator
{
    public partial class Form1 : Form
    {

        CrmServiceOptimizer cso;


        public Form1()
        {
            InitializeComponent();
        }



        public void log(string message, bool newLine = true)
        {
            if (newLine)
            {
                txtLog.AppendText(DateTime.Now.ToString("HH:mm:ss.ff") + " " + message + Environment.NewLine);
            }
            else
            {
                txtLog.AppendText(DateTime.Now.ToString("HH:mm:ss.ff") + " " + message);
            }



            txtLog.SelectionStart = txtLog.TextLength;
            txtLog.ScrollToCaret();

            Application.DoEvents();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // hide un-implemented functionality
            btnSettings.Visible = false;
            //

            CmbLanguage.SelectedIndex = 0;
            CmbVersion.SelectedIndex = 0;
            log("Loading Message defs...", false);

            if (!File.Exists("Messages.xml"))
            {
                File.WriteAllText("Messages.xml", Properties.Resources.messagesXml);
            }

            if (!File.Exists("BackendTypes.xml"))
            {
                File.WriteAllText("BackendTypes.xml", Properties.Resources.BackendTypesXml);
            }

            if (!File.Exists("SchedulingTypes.xml"))
            {
                File.WriteAllText("SchedulingTypes.xml", Properties.Resources.SchedulingTypesXml);
            }

            cso = new CrmServiceOptimizer("Messages.xml", "BackendTypes.xml", "SchedulingTypes.xml");
            cso.txtLog = txtLog;

            //LoadMessageDefs();
            log(" Done");

            grpEntities.Enabled = false;
            grpCompile.Enabled = false;

            radioUrl.Checked = true;

            LockDown();

        }

        private void btnWsdl_Click(object sender, EventArgs e)
        {

            if (radioFile.Checked)
            {
                if (File.Exists(txtFileWsdl.Text))
                {
                    log("Opening wsdl");
                    cso.LoadFile(txtFileWsdl.Text);
                }
                else
                {
                    MessageBox.Show("Could not find wsdl file");
                }
            }
            else
            {
                //attempt to download the wsdl
                log("Downloading wsdl");
                try
                {
                    cso.LoadUrl(TxtUrl.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

            UnLockDown();

            ListRemoveEntities.DataSource = cso.RemoveEntities;
            ListKeepEntities.DataSource = cso.KeepEntities;

            grpEntities.Enabled = true;
            grpCompile.Enabled = true;
            updateUI();


        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (string s in ListRemoveEntities.SelectedItems)
            {
                cso.AddRequiredEntity(s);
            }

            updateUI();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            foreach (string s in ListKeepEntities.SelectedItems)
            {
                cso.RemoveRequiredEntity(s);
            }

            updateUI();
        }

        private void updateUI()
        {
            txtKeepCount.Text = cso.KeepEntities.Count().ToString();
            txtRemoveCount.Text = cso.RemoveEntities.Count().ToString();
            ListKeepEntities.DataSource = cso.KeepEntities;
            ListRemoveEntities.DataSource = cso.RemoveEntities;

            //((CurrencyManager)ListKeepEntities.BindingContext[cso.KeepEntities]).Refresh();
            //((CurrencyManager)ListRemoveEntities.BindingContext[cso.RemoveEntities]).Refresh();
        }

        private void LockDown()
        {
            btnAdd.Enabled = false;
            btnRemove.Enabled = false;
            checkBackend.Enabled = false;
            checkSched.Enabled = false;
        }

        private void UnLockDown()
        {
            btnAdd.Enabled = true;
            btnRemove.Enabled = true;
            checkBackend.Enabled = true;
            checkSched.Enabled = true;
        }



        private void btnImport_Click(object sender, EventArgs e)
        {
            cso.GenerateCode = checkGenCode.Checked;
            cso.Compile64 = check64.Checked;
            cso.AssemblyName = txtNamespace.Text;
            cso.EmbedXmlSerializers = checkEmbedSerializers.Checked;
            cso.FrameworkVersion = CmbVersion.Text;
            cso.GenerateLanguage = CmbLanguage.Text;
            cso.RemoveBackendTypes = checkBackend.Checked;
            cso.RemoveScedulingTypes = checkSched.Checked;
            cso.Optimize();
        }


        private void checkGenCode_CheckedChanged(object sender, EventArgs e)
        {
            if (checkGenCode.Checked)
            {
                CmbLanguage.Enabled = true;
            }
            else
            {
                CmbLanguage.Enabled = false;
            }
        }

        private void btnViewTypes_Click(object sender, EventArgs e)
        {
            //UpdateTypesLists();


            //using (FormList fl = new FormList())
            //{
            //    fl.listBox.DataSource = TypesToDelete;
            //    fl.txtRemove.Text = TypesToDelete.Count.ToString();
            //    fl.ShowDialog();
            //}


        }



        private void check64_CheckedChanged(object sender, EventArgs e)
        {
            if (check64.Checked)
            {
                DialogResult r = MessageBox.Show("Default behavior is AnyCpu - are you sure you want to force 64-bit?", "Force 64 bit", MessageBoxButtons.YesNo);
                if (r == System.Windows.Forms.DialogResult.No)
                {
                    check64.Checked = false;
                }
            }
        }

        private void radioUrl_CheckedChanged(object sender, EventArgs e)
        {
            if (radioUrl.Checked)
            {
                TxtUrl.Enabled = true;
                txtFileWsdl.Enabled = false;
                btnWsdlBrowse.Enabled = false;
            }
            else
            {
                TxtUrl.Enabled = false;
                txtFileWsdl.Enabled = true;
                btnWsdlBrowse.Enabled = true;
            }
        }

        private void btnWsdlBrowse_Click(object sender, EventArgs e)
        {
            //XmlSerializer x = new XmlSerializer(typeof(List<string>));
            //TextWriter tw = new StreamWriter("types.xml");

            //x.Serialize(tw, TypesToDelete);
            //tw.Close();

        }

    }









}
